package hos.map.geometry;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import hos.map.coordinate.CoordinateType;
import hos.map.coordinate.LatLng;
import hos.map.coordinate.LatLngCollection;

/**
 * <p>Title: Polygon </p>
 * <p>Description: 面 </p>
 * <p>Company: www.mapuni.com </p>
 *
 * @author : 蔡俊峰
 * @version : 1.0
 * @date : 2023-12-07 22:35
 */
public class Polygon extends GeometryCollection {

    public Polygon(CoordinateType type) {
        super(type);
    }

    public Polygon(LatLngCollection list, CoordinateType type) {
        super(list, type);
    }

    public Polygon(LatLng... points) {
        super(points);
    }

    public Polygon(Collection<LatLng> points) {
        super(points);
    }

    public Polygon(Collection<LatLng> points, CoordinateType type) {
        super(points, type);
    }

    public static Polygon fromMap(List<Map<String, Double>> list, CoordinateType type) {
        if (list == null) {
            return null;
        }
        List<LatLng> pointList = new ArrayList<>();
        for (Map<String, Double> item : list) {
            pointList.add(LatLng.fromMap(item, type));
        }
        return new Polygon(pointList, type);
    }

    public static Polygon fromMap(Map<String, Object> dataMap, CoordinateType type) {
        if (dataMap == null) {
            return null;
        }
        Object objList = dataMap.get("list");
        if (objList == null) {
            return null;
        }
        if (!(objList instanceof List)) {
            return null;
        }
        if (type == null) {
            String typeName = (String) dataMap.get("type");
            type = CoordinateType.form(typeName);
        }
        @SuppressWarnings("unchecked")
        List<Map<String, Object>> list = (List<Map<String, Object>>) objList;
        List<LatLng> pointList = new ArrayList<>();
        for (Map<String, Object> item : list) {
            pointList.add(LatLng.fromMapLatLng(item, type));
        }
        Polygon polygon = new Polygon(pointList, type);
        if (dataMap.containsKey("json")) {
            polygon.setJson((String) dataMap.get("json"));
        }
        if (dataMap.containsKey("id")) {
            polygon.id = (String) dataMap.get("id");
        }
        return polygon;
    }

    @Override
    public Polygon toBaiDu() {
        if (mList == null || mList.isEmpty()) {
            return this;
        }
        List<LatLng> targetList = new ArrayList<>();
        for (LatLng latLng : mList) {
            targetList.add(latLng.toBaiDu());
        }
        return new Polygon(targetList, CoordinateType.bd09mc);
    }

    @Override
    public Polygon toAMap() {
        if (mList == null || mList.isEmpty()) {
            return this;
        }
        List<LatLng> targetList = new ArrayList<>();
        for (LatLng latLng : mList) {
            targetList.add(latLng.toAMap());
        }
        return new Polygon(targetList, CoordinateType.gcj02);
    }

    @Override
    public Polygon toWgs() {
        if (mList == null || mList.isEmpty()) {
            return this;
        }
        List<LatLng> targetList = new ArrayList<>();
        for (LatLng latLng : mList) {
            targetList.add(latLng.toWgs());
        }
        return new Polygon(targetList, CoordinateType.wgs84);
    }

    public Polyline toPolyline() {
        if (mList == null) {
            mList = new LatLngCollection(getType());
        }
        return mList.toPolyline();
    }


}
